/*
* @(#) ThreadTest.java 2018年02月28日
*
* Copyright 2010 NetEase.com, Inc. All rights reserved.
*/
package com.leo.m1802.d28;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * @author xuexiaolei
 * @version 2018年02月28日
 */
public class ThreadTest {
    private static CountDownLatch latch = new CountDownLatch(5);

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        exec.submit(new Task());
        exec.submit(new Task());
        exec.submit(new Task());
        exec.submit(new Task());
        exec.submit(new Task());
        latch.await();
        System.out.println("都执行完了，我总结下");
    }

    private static class Task implements Runnable{
        @Override public void run() {
            try {
                Thread.sleep(new Random().nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我完成了任务");
            latch.countDown();
        }
    }
}
